import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CustomerSet {
    List<Customer> customerList = new ArrayList<Customer>();
    GoodsSet goodsSet = new GoodsSet();
    Customer user = null;
    public void menu(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请选择要进行的操作");
            System.out.println("1--注册");
            System.out.println("2--登录");
            System.out.println("3--密码管理");
            System.out.println("4--购物");
            System.out.println("5--退出登录/返回上一级");
            System.out.println("6--忘记密码。");
            int choose = 0;
            try{
                choose = scanner.nextInt();
            }catch (Exception e){
                System.out.println("输入不合法，请重新输入。");
                while (scanner.hasNext()) {
                    if (scanner.hasNextLine()) {
                        scanner.nextLine();
                        break;
                    } else {
                        scanner.next();
                    }
                }
                menu();
                return;
            }
            switch (choose){
                case 1:
                    register();
                    break;
                case 2:
                    logIn();
                    break;
                case 3:
                    if(user == null){
                        System.out.println("请先登录账户");
                        System.out.println("输入任意字符继续");
                        scanner.next();
                        break;
                    }
                    user.passwordManagement();
                    break;
                case 4:
                    if(user == null){
                        System.out.println("请先登录账户");
                        System.out.println("输入任意字符继续");
                        scanner.next();
                        break;
                    }
                    user.shoppingManagement();
                    break;
                case 5:
                    logOut();
                    return;
                case 6:
                    forgetPassword();
                    break;
                default:
                    System.out.println("输入不合法，请重新输入。");
                    break;
            }
        }

    }
    public void logOut(){
        System.out.println("已成功退出登录！！");
        user = null;
    }
    public void forgetPassword(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String account = scanner.next();
        Customer customer = null;
        for(Customer cus : customerList){
            if(cus.getAccount().equals(account)){
                customer = cus;
            }
        }
        if(customer == null){
            System.out.println("不存在该账户，输入任意字符继续");
            scanner.next();
            return;
        }
        customer.forgetPassword();
    }
    public void register(){
        Scanner input = new Scanner(System.in);
        while(true){
            System.out.println("请输入用户名：（长度不少于5个字符）");
            String account = input.next();
            if(account.length() < 5){
                System.out.println("用户名长度不少于5个字符，请重新注册。");
                continue;
            }
            System.out.println("请输入密码（密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合。）：");
            String password = input.next();
            if(!checkPassword(password)){
                System.out.println("密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合。请重新注册。");
                continue;
            }
            Customer cus = new Customer(account, password,goodsSet);
            System.out.println("请输入邮箱");
            cus.setMailbox(input.next());
            while(true){
                System.out.println("请设置一个ID号");
                String id = input.next();
                cus.setID(id);
                boolean IDFlag = true;
                for(Customer customer : customerList){
                    if(id.equals(customer.ID)){
                        IDFlag = false;
                    }
                }
                if(IDFlag)break;
                System.out.println("此ID号已经被注册，请重新输入");
                System.out.println("输入任意字符继续");
                input.next();
            }
            customerList.add(cus);
            System.out.println("注册成功！！\n输入任意字符返回。");
            input.next();
            break;
        }
    }

    public Customer logIn(){
        Scanner input = new Scanner(System.in);
        if(user != null){
            System.out.println("您已经登录！输入任意字符继续");
            input.next();
            return null;
        }
        while(true){
            System.out.println("请输入账户/用户名");
            String account = input.next();
            int idx = 0;
            for(;idx < customerList.size(); idx++){
                if(customerList.get(idx).getAccount().equals(account)){
                    if(customerList.get(idx).lockFlag){
                        System.out.println("该账户已被锁定，无法登录");
                        return null;
                    }
                    break;
                }
            }
            if(idx==customerList.size()){
                System.out.println("不存在该账户");
                System.out.println("输入任意字符返回上一级");
                input.next();
                return null;
            }
            System.out.println("请输入密码");
            for(int time = 4; time >= 0; time--){
                String password = input.next();
                if(password.equals(customerList.get(idx).getPassword())){
                    System.out.println("登录成功！！");
                    user = customerList.get(idx);
                    return customerList.get(idx);
                }
                if(time > 0){
                    System.out.println("密码输入错误，请重新输入，剩余输入次数："+time);
                }else{
                    System.out.println("密码输入错误，您的账户已被锁定。");
                    customerList.get(idx).lockFlag = true;
                }
            }
            break;
        }
        return null;
    }
    private boolean checkPassword(String password){
        if(password.length() < 8)return false;
        boolean hasUpperCase = false;
        boolean hasLowerCase = false;
        boolean hasDigit = false;
        boolean hasSpecialChar = false;

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);
            if (Character.isUpperCase(ch)) {
                hasUpperCase = true;
            } else if (Character.isLowerCase(ch)) {
                hasLowerCase = true;
            } else if (Character.isDigit(ch)) {
                hasDigit = true;
            } else{
                hasSpecialChar = true;
            }
        }
        return hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar;
    }
}
